﻿using SH.Model;

namespace SH.DataAccess.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    public sealed class Configuration : DbMigrationsConfiguration<ModelContainer>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(ModelContainer context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );

            const string staUsual = "Звичайний";
            const string staCanceled = "Анульований";
            const string staSuspensed = "Тимчасово припинений";

            context.Statuses.AddOrUpdate(
                  p => p.Name,
                  new Status { Name = staUsual },
                  new Status { Name = staCanceled },
                  new Status { Name = staSuspensed }
                );
            context.SaveChanges();
            
            context.Regions.AddOrUpdate(
                  p => p.Name,
                  //new Region { Name = context.GetResourceString("АР Крым", typeof(Resource_RU)) },
                  new Region { Name = "АР Крым" },
                  new Region { Name = "Винницкая" },
                  new Region { Name = "Волынская" },
                  new Region { Name = "Днепропетровская" },
                  new Region { Name = "Донецкая" },
                  new Region { Name = "Житомирская" },
                  new Region { Name = "Закарпатская" },
                  new Region { Name = "Запорожская" },
                  new Region { Name = "Ивано-Франковская" },
                  new Region { Name = "Киев" },
                  new Region { Name = "Киевская" },
                  new Region { Name = "Кировоградская" },
                  new Region { Name = "Луганская" },
                  new Region { Name = "Львовская" },
                  new Region { Name = "Николаевская" },
                  new Region { Name = "Одесская" },
                  new Region { Name = "Полтавская" },
                  new Region { Name = "Ровенская" },
                  new Region { Name = "Севастополь" },
                  new Region { Name = "Сумская" },
                  new Region { Name = "Тернопольская" },
                  new Region { Name = "Харьковская" },
                  new Region { Name = "Херсонская" },
                  new Region { Name = "Хмельницкая" },
                  new Region { Name = "Черкасская" },
                  new Region { Name = "Черниговская" },
                  new Region { Name = "Черновицкая" }
                );
            context.SaveChanges();
            
            context.Storehauses.AddOrUpdate(
                  p => p.Edrpou,
                  new Storehause { Edrpou = 12345678, Name = "Склад \"Лампочка ильича\"", Capacity = 100000, Region = context.Regions.First(x => x.Id == 1) },
                  new Storehause { Edrpou = 34542345, Name = "Мега-склад", Capacity = 100, Region = context.Regions.First(x => x.Id == 2) },
                  new Storehause { Edrpou = 23452111, Name = "Секретный склад", Capacity = 54000, Region = context.Regions.First(x => x.Id == 6) },
                  new Storehause { Edrpou = 23141242, Name = "Подземный склад", Capacity = 542300, Region = context.Regions.First(x => x.Id == 8) },
                  new Storehause { Edrpou = 23411555, Name = "Старый склад", Capacity = 432540, Region = context.Regions.First(x => x.Id == 1) },
                  new Storehause { Edrpou = 41243124, Name = "Склад хелпдеска", Capacity = 200, Region = context.Regions.First(x => x.Id == 1) }
                );
            context.SaveChanges();
            
            context.Certificates.AddOrUpdate(
                p => p.Id,
                //                p => new { p.Storehause.Edrpou, p.Status },
                new Certificate
                {
                    DateStart = DateTime.Now,
                    DateEnd = DateTime.Now.AddYears(1),
                    //                    Status = context.Statuses.Find(1),
                    Status = context.Statuses.First(status => status.Name == staUsual),
                    Storehause = context.Storehauses.First(storehause => storehause.Edrpou == 12345678)
                },
                new Certificate
                {
                    DateStart = DateTime.Now.AddDays(2),
                    DateEnd = DateTime.Now.AddYears(2),
                    Status = context.Statuses.First(status => status.Name == staUsual),
                    Storehause = context.Storehauses.First(storehause => storehause.Edrpou == 34542345)
                },
                new Certificate
                {
                    DateStart = DateTime.Now.AddDays(2),
                    DateEnd = DateTime.Now.AddYears(2),
                    Status = context.Statuses.First(status => status.Name == staCanceled),
                    Storehause = context.Storehauses.First(storehause => storehause.Edrpou == 23411555)
                }
                );
            context.SaveChanges();
            
        }
    }
}
